﻿using System.Collections.Generic;
using ProxyTool.Models;
using ProxyTool.Utils;

namespace ProxyTool.Services
{
    /// <summary>
    ///     调试服务
    /// </summary>
    public static class DebugService
    {
        /// <summary>
        ///     WSS系统服务地址
        /// </summary>
        public const string SvcURI = @"/wss/Debug.svc";

        /// <summary>
        ///     测试连接
        /// </summary>
        public static bool TestConnection(string u9URI)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "TestConnection";
            const string serviceActionName = "测试连接";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        #region 事务调试

        /// <summary>
        ///     是否事务调试中
        /// </summary>
        public static bool IsTransactionDebugging(string u9URI, int debugType, string proxyType = "")
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "IsTransactionDebugging";
            const string serviceActionName = "是否事务调试中";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("debugType", debugType);
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     启用事务调试
        /// </summary>
        public static bool SetupTransactionDebug(string u9URI, int debugType, string proxyType = "")
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "SetupTransactionDebug";
            const string serviceActionName = "启用事务调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("debugType", debugType);
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     停止事务调试
        /// </summary>
        public static bool StopTransactionDebug(string u9URI, int debugType, string proxyType = "")
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "StopTransactionDebug";
            const string serviceActionName = "停止事务调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("debugType", debugType);
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     停止所有事务调试
        /// </summary>
        public static bool StopAllTransactionDebug(string u9URI)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "StopAllTransactionDebug";
            const string serviceActionName = "停止所有事务调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        #endregion

        #region 代理调试

        /// <summary>
        ///     是否代理Do方法调试中
        /// </summary>
        public static bool IsProxyDoDebugging(string u9URI, string proxyType)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "IsProxyDoDebugging";
            const string serviceActionName = "是否代理Do调试中";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     启用代理Do调试
        /// </summary>
        public static bool SetupProxyDoDebug(string u9URI, string proxyType)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "SetupProxyDoDebug";
            const string serviceActionName = "启用代理Do调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     停止代理Do调试
        /// </summary>
        public static bool StopProxyDoDebug(string u9URI, string proxyType)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "StopProxyDoDebug";
            const string serviceActionName = "停止代理Do调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            postData.Add("proxyType", proxyType);
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        /// <summary>
        ///     停止所有代理Do方法调试
        /// </summary>
        public static bool StopAllProxyDoDebug(string u9URI)
        {
            if (string.IsNullOrWhiteSpace(u9URI))
                throw new ProxyToolException("U9(C)地址 不能为空，请检查配置");
            string systemServiceURI = u9URI + SvcURI;
            const string serviceAction = "StopAllProxyDoDebug";
            const string serviceActionName = "停止所有代理Do调试";
            Dictionary<string, object> postData = new Dictionary<string, object>();
            postData.Add(ServiceHelper.ContextName, ContextInfo.GetContext());
            return ServiceHelper.DoRequest<bool>(systemServiceURI, serviceAction, serviceActionName, postData);
        }

        #endregion
    }
}